package handler;

import beans.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import service.adminService;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class adminHandler {
    @Autowired
    private adminService admin;

    @RequestMapping("/selectUsers")
    public String selectUsers(Model model){
        List<User> users= admin.selectUsers();
        model.addAttribute("users",users);
        return "admin/inquire_employee";
    }

    @RequestMapping("/addUser")
    public String addUser(User user, Model model){
        admin.addUser(user);
        return this.selectUsers(model);
    }

    @RequestMapping("/openUserDetail")
    public String openUserDetail(Integer userId,Model model){
        User user=admin.selectUserById(userId);
        model.addAttribute("user",user);
        return "admin/user_information";
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(Integer userId,Model model){
        admin.deleteUserById(userId);
        return this.selectUsers(model);
    }

    @RequestMapping("/divideUser")
    public String divideUser(Model model){
        List<User> users= admin.selectUsers();
        model.addAttribute("users",users);
        return "admin/divide_out";
    }

    @RequestMapping("/openDivide")
    public String openDivide(Integer userId,Model model){
        User user=admin.selectUserById(userId);
        List<User> users= admin.selectUsers();
        model.addAttribute("users",users);
        model.addAttribute("user",user);
        return "admin/divide_user";
    }

    @RequestMapping("/addUserSuperId")
    public String addUserSuperId(@Param("userId") Integer userId,@Param("super_id") Integer super_id, Model model){
        admin.addUserSuperId(super_id,userId);
        return this.selectUsers(model);
    }

}
